import Scene from "../Scene";
import UIMrg from "../../Script/UIMrg";
import MainPanel from "../../Script/MainPanel";
import { TButton, TLayout } from "../SceneControl";
const { ccclass, property } = cc._decorator;

@ccclass
export default class MainPageScene extends Scene {
    mainPanel: any;

    protected onEnable(): void {
        super.onEnable();
        this.mainPanel = UIMrg.UIMrg.getPanel(MainPanel.MainPanel);
        this.initScene()
    }

    public initScene() {
        this.tscene.addLayout(this.getUpLayout(), { row: 0, line: 0 })
        this.tscene.addLayout(this.getBottonLayout(), { row: 1, line: 0 })
        this.tscene.site.row = 1;
        this.tscene.onBack = () => {
            this.mainPanel.view.getChildByName("exitPop").active = true;
        };
        this.tscene.joinController();
    }

    getBottonLayout(): TLayout {
        let bottonLayout = new TLayout("bottonLayout");
        let nodes = [this.mainPanel._btnStore, this.mainPanel._btnCivilization, this.mainPanel._btnBattle, this.mainPanel._btnHero, this.mainPanel._btnCard]
        let btns: TButton[][] = [
            nodes.map((node) => new TButton(node.view))
        ]
        let index = nodes.findIndex((node) => node.selected)
        if (index == -1) index = 2;
        bottonLayout.buttonList = btns;
        bottonLayout.site.line = index
        return bottonLayout;
    }

    getUpLayout(): TLayout {
        let upLayout = new TLayout(this.node.name);
        let nodes = Scene.getAllMatchingChildren(this.node);
        let btns = Scene.sortNodesIntoGrid(nodes, 100, true) as TButton[][];
        upLayout.buttonList = btns;
        upLayout.site.row = btns.length - 1;
        return upLayout;
    };
}
